package cerebrum.crawler;

import cerebrum.common.CommonConst;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * 诗词爬虫
 */
public class VerseWebCrawler {
    /**
     * 根据类型爬取相应的诗句
     *
     * @param msg
     * @return
     */
    public static List<List<String>> getVerseMsgs(String msg) {
        msg = CommonConst.VERSE_URL + msg;
        int i = webCrawlerHtmlNum(msg);
        List<List<String>> list = new ArrayList<>();
        if (i > 0) {
            for (int j = 0; j < i; j++) {
                List<List<String>> list1 = webCrawlerHtmlAnalysis(msg);
                list.addAll(list1);
            }

        }
        return list;
    }


    /**
     * 获取网页页码，页数
     *
     * @param url
     * @return
     */
    public static int webCrawlerHtmlNum(String url) {
        try {
            Document document = CrawlerUtil.webCrawlerHTTP(url);
            Element body = document.body();

            Element fromPage = body.getElementById("sumPage");
            String pageNum = fromPage.ownText();
            return Integer.valueOf(pageNum);
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    /**
     * 爬取解析
     *
     * @param url
     * @return
     */
    private static List<List<String>> webCrawlerHtmlAnalysis(String url) {
        try {
            Document document = CrawlerUtil.webCrawlerHTTP(url);

            Element body = document.body();
            Elements sons1 = body.getElementsByClass("sons");
            Element element = sons1.get(0);
            Elements cont = element.getElementsByClass("cont");
            List<List<String>> list = new ArrayList<>();
            for (Element elements : cont) {
                Elements cont1 = elements.getElementsByClass("cont");
                Elements a = cont1.get(0).getElementsByTag("a");
                Element element1 = a.get(0);
                List<String> li = new ArrayList<String>();
                if (a.size() > 1) {
                    Element element2 = a.get(1);

                    if (element2 != null) {
                        String[] strip = element2.ownText().split("《", 2);
                        li.add(strip[0]);
                    }
                }
                if (element1 != null) {
                    li.add(element1.ownText());
                }
                list.add(li);
            }
            return list;
        } catch (IOException e) {
            e.printStackTrace();
            return new ArrayList<>();
        }
    }


}
